# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/3 下午 03:26
# @Author   : Li Jie
# @Site     : http://www.hzdledu.cn/
# @File     : main_page.py
# @Software : PyCharm
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait  # 等待类
from selenium.webdriver.support import expected_conditions  # 等待条件
from selenium.webdriver.common.by import By  # 定位符


class MainPage():
    def __init__(self, driver):
        self.icon_locator = (By.XPATH, '//*[@id="root"]/section/section/header/div/div[2]/span/span[1]')  # 头像定位符
        # self.logout_locator = (By.XPATH, '/html/body/div[3]/div/div/ul/li[3]')  # 退出登录定位符
        self.logout_locator = (By.XPATH,'//li[text()="退出登录"]')

        self.driver = driver
        self.wait = WebDriverWait(self.driver, 30)

        # self.driver = webdriver.Chrome()  # 调试使用

    def move_to_icon(self):
        """
        移动鼠标到头像
        :return: None
        """
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.icon_locator))
            ActionChains(self.driver).move_to_element(self.driver.find_element(*self.icon_locator)).perform()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/移动鼠标到头像错误.png')
            raise e

    def click_logout(self):
        """
        点击退出登录
        :return: None
        """
        try:
            # time.sleep(2)
            self.wait.until(expected_conditions.presence_of_element_located(self.logout_locator))
            self.driver.find_element(*self.logout_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/点击退出登录错误.png')
            raise e